<?php
namespace app\admin\controller;

use catetree\Catetree;
use think\Controller;

class Category extends Controller
{
    public function lst()
    {
        $category    = new Catetree();
        $categoryObj = db('category');

        if (request()->isPost()) {
            $data = input('post.');
            $category->cateSort($data['sort'], $categoryObj);
            $this->success('排序成功', url('lst'));
        }
        $categoryRes = $categoryObj->order('sort DESC')->select();
        $categoryRes = $category->Catetree($categoryRes);

        $this->assign([
            'categoryRes' => $categoryRes,

        ]);
        return view('list');
    }
    public function add()
    {
        $category    = new Catetree();
        $categoryObj = model('category');
        if (request()->isPost()) {
            $data = input('post.');

            //处理图片上传
            if ($_FILES['cate_img']['tmp_name']) {
                $data['cate_img'] = $this->upload();
            }

            // //验证
            // $validate = validate('category');
            // if(!$validate->check($data)){
            //     $this->error($validate->getError());

            // }
            $add = $categoryObj->save($data);

            if ($add) {

                $this->success('添加分类成功 ！', 'lst');
            } else {

                $this->error('添加分类失败');
            }
            return;
        }

        $categoryRes = $categoryObj->order('sort ACS')->select();
        $categoryRes = $category->Catetree($categoryRes);
        //栏目推荐位
        $categoryRecposRes = db('recpos')->where('rec_type', '=', 2)->select();
        $this->assign([
            'categoryRes'       => $categoryRes,
            'categoryRecposRes' => $categoryRecposRes,

        ]);

        return view();
    }
    public function edit()
    {
        $category    = new Catetree();
        $categoryObj = model('category');
        if (request()->isPost()) {
            $data = input('post.');
            if ($_FILES['cate_img']['tmp_name']) {
                $oldBrands    = $categoryObj->find($data['id']);
                $oldBrandsImg = IMG_UPLOADS . $oldBrands['cate_img'];
                if (file_exists($oldBrandsImg)) {
                    @unlink($oldBrandsImg);
                }
                $data['cate_img'] = $this->upload();
            }

            //验证
            // $validate = validate('category');
            // if(!$validate->check($data)){
            //     $this->error($validate->getError());

            // }

            $save = $categoryObj->update($data);
            if ($save !== false) {

                $this->success('修改分类成功 ！', 'lst');
            } else {

                $this->error('修改分类失败');
            }
            return;
        }

        $categoryRes = $categoryObj->order('sort ACS')->select();
        $categoryRes = $category->Catetree($categoryRes);

        $categorys = $categoryObj->find(input('id'));
        //栏目推荐位
        $categoryRecposRes = db('recpos')->where('rec_type', '=', 2)->select();
        //更改栏目推荐位
        $_mycategoryRecposRes = db('rec_item')->where(array('value_type' => 2, 'value_id' => input('id')))->select();
        //改写二维数组为一维数组
        $mycategoryRecposRes = array();
        foreach ($_mycategoryRecposRes as $k => $v) {
            $mycategoryRecposRes[] = $v['recpos_id'];
        }

        $this->assign([
            'categoryRes'         => $categoryRes,
            'categorys'           => $categorys,
            'categoryRecposRes'   => $categoryRecposRes,
            'mycategoryRecposRes' => $mycategoryRecposRes,

        ]);

        return view();
    }

    public function del($id)
    {
        $category = db('category');
        $Catetree = new Catetree();
        $sonids   = $Catetree->childrenids($id, $category);
        $sonids[] = intval($id);

        //删除分类前判断该分类下的文章和文章相关缩略图
        // $article=db('article');
        // foreach ($sonids as $k => $v) {
        //     $artRes=$article->field('id,thumb')->where(array('category_id'=>$v))->select();
        //     foreach ($artRes as $k1 => $v1) {
        //         $thumbSrc=IMG_UPLOADS.$v1['thumb'];
        //         if (file_exists($thumbSrc)) {
        //             @unlink($thumbSrc);
        //         }
        //         $article->delete($v1['id']);
        //     }
        // }

        $del = $category->delete($sonids);
        if ($del) {
            $this->success('删除分类成功！', 'lst');
        } else {
            $this->error('删除分类失败！');
        }
    }

    public function upload()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('cate_img');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'uploads');
            if ($info) {

                return $info->getSaveName();
            } else {
                // 上传失败获取错误信息
                echo $file->getError();
                die;
            }
        }
    }
}
